package com.jueling.culture.controller;

import com.jueling.culture.core.Result;
import com.jueling.culture.core.ResultGenerator;
import com.jueling.culture.dto.req.RoleInfoDto;
import com.jueling.culture.exception.FrontShowException;
import com.jueling.culture.model.RoleInfo;
import com.jueling.culture.service.RoleInfoService;
import com.jueling.culture.support.annotation.RequiredPermission;
import com.jueling.culture.support.enums.AuthorityCodeEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* @author linp
* @date 2019/06/10
*/
@RestController
@RequestMapping("/api")
public class RoleInfoController {
    @Resource
    private RoleInfoService roleInfoService;

    /**
     * admin用的id
     */
    private static final Integer ADMIN_ID = 1;

    /**
     * update用的id
     */
    private static final Integer UPDATE_ID = 2;

    /**
     * 角色新增
     * @param roleInfoDto
     * @return
     */
    @PostMapping("/sysroles")
    @RequiredPermission(authorityCodeEnum = AuthorityCodeEnum.ROLE_MANEGE,edit = true)
    public Result add(@RequestBody RoleInfoDto roleInfoDto) {
        roleInfoService.addRoleInfo(roleInfoDto);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 角色删除
     * @param roleId
     * @return
     */
    @DeleteMapping("/sysroles/{roleId}")
    @RequiredPermission(authorityCodeEnum = AuthorityCodeEnum.ROLE_MANEGE,edit = true)
    public Result delete(@PathVariable String roleId) {
        int role = Integer.valueOf(roleId).intValue();
        if(role == ADMIN_ID || role == UPDATE_ID){
            throw new FrontShowException("该角色模板为基础模板，不可删除");
        }
        roleInfoService.deleteRoleById(roleId);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 角色编辑
     * @param roleId
     * @param roleInfoDto
     * @return
     */
    @PutMapping("/sysroles/{roleId}")
    @RequiredPermission(authorityCodeEnum = AuthorityCodeEnum.ROLE_MANEGE,edit = true)
    public Result update(@PathVariable String roleId, @RequestBody RoleInfoDto roleInfoDto) {
        int role = Integer.valueOf(roleId).intValue();
        if(role == ADMIN_ID){
            throw new FrontShowException("该角色模板为基础模板，不可编辑");
        }
        roleInfoService.editRoleInfo(roleId,roleInfoDto);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 获取角色详情
     * @param roleId
     * @return
     */
    @GetMapping("/sysroles/{roleId}")
    @RequiredPermission(authorityCodeEnum = AuthorityCodeEnum.ROLE_MANEGE,view = true)
    public Result detail(@PathVariable String roleId) {
        RoleInfo roleInfo = roleInfoService.getRoleInfoById(roleId);
        return ResultGenerator.genSuccessResult(roleInfo);
    }

    /**
     * 获取角色列表
     * @return
     */
    @GetMapping("/sysroles")
    @RequiredPermission(authorityCodeEnum = AuthorityCodeEnum.ROLE_MANEGE,view = true)
    public Result list() {
        List<RoleInfo> list = roleInfoService.getRoleInfoList();
        return ResultGenerator.genSuccessResult(list);
    }

    /**
     * 判断角色名是否存在
     * @return
     */
    @GetMapping("/check/sysroles/{roleName}")
    @RequiredPermission(authorityCodeEnum = AuthorityCodeEnum.ROLE_MANEGE,view = true)
    public Result checkRole(@PathVariable String roleName){
        boolean check = roleInfoService.checkRole(roleName);
        return ResultGenerator.genSuccessResult(check);
    }

}
